package com.example.musicplayer.model;
import java.util.*;
import android.os.*;
import java.io.*;

public class MusicLrc
{
	private List<HashMap<String,String>>musicLrcList=new ArrayList<HashMap<String,String>>();//歌词
	private String musicLrcContent;//单句歌词
	private String fileName;//文件名
	
	//设置文件名
	public void setFileName(String fileName)
	{
		this.fileName = fileName;
		musicLrcList.clear();//清除歌词列表
		String path=Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator +"Music" + File.separator + "Musiclrc";
		File file=new File(path + File.separator + fileName+".lrc");
		if (file.exists())
		{
			
			BufferedReader br=null;
			try
			{
				br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));//新建BufferedReader对象
				String line=null;
				while((line=br.readLine())!=null){
					int index=line.indexOf("]");
					String time=line.substring(1,index);//截取歌词时间
					String content=line.substring(index+1);//截取歌词
					HashMap<String,String> hashMap=new HashMap<String,String>();//新建HashMap
					//组装对象
					hashMap.put("time",time);
					hashMap.put("content",content);
					musicLrcList.add(hashMap);//添加HashMap对象
				}
			}
			catch (FileNotFoundException e)
			{}
			catch (IOException e)
			{}
			finally
			{
				if (br != null)
				{
					try
					{
						br.close();
					}
					catch (IOException e)
					{}
				}
			}
		}
	}

	//获取文件名
	public String getFileName()
	{
		return fileName;
	}

	//获取歌词列表
	public List<HashMap<String, String>> getMusicLrcList()
	{
		return musicLrcList;
	}
	
	//通过时间获取歌词
	public String getMusicLrcContentByTime(long time){
		
		Date date=new Date(time);
		String str=String.format("%tM:%tS:%tN",date,date,date);
		str=str.substring(0,str.length()-1);
		for(int i=1;i<musicLrcList.size();i++){
			HashMap<String,String> h=musicLrcList.get(i-1);
			HashMap<String,String> h1=musicLrcList.get(i);
			if(h.get("time").compareTo(str)<=0&&h1.get("time").compareTo(str)>0){
				musicLrcContent=h.get("content");
				//musicLrcList.remove(i);
				return musicLrcContent;
			}
		}
		return null;
	}
}
